//
//  HYLoginViewModel.m
//  Agency
//
//  Created by 胡勇 on 2017/11/1.
//  Copyright © 2017年 胡勇. All rights reserved.
//

#import "HYLoginViewModel.h"

@interface HYLoginViewModel()

@property (nonatomic, strong) RACSignal *phoneSignal;
@property (nonatomic, strong) RACSignal *passwordSignal;

@end

@implementation HYLoginViewModel

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self initializeSignal];
    }
    return self;
}

- (void)initializeSignal{
    
    _phoneSignal = RACObserve(self, phone);
    _passwordSignal = RACObserve(self, password);
    
    _loginSuccessSubject = [RACSubject subject];
    _loginErrorSubject = [RACSubject subject];
    _agreementDetailSubject = [RACSubject subject];
}

- (void)dealloc{
    
}

#pragma mark - public method
- (RACSignal *)loginButtonIsValid{
    
    RACSignal *isValid = [RACSignal combineLatest:@[_phoneSignal,_passwordSignal] reduce:^id{
       
        return @(_phone.length >= 3 && _password.length >= 3);
    }];
    
    return isValid;
}

- (void)loginAction{
    
    [HYNetRequestHandle loginWithPhone:self.phone password:self.password complecton:^(BOOL isSuccess) {
       
        if (isSuccess) {

            [self.loginSuccessSubject sendNext:@"ok"];
        }
    }];
    
}

- (void)lookAgreementDetailAction{
    [self.agreementDetailSubject sendNext:nil];
}



#pragma mark - privateMethod
/** 判断手机号是否有效 */
- (BOOL)validatePhoneNum:(NSString *)phoneNum{
    
    NSString *numReg = @"^(1)\\d{10}$";
    NSPredicate *numCheck = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",numReg];
    
    return [numCheck evaluateWithObject:phoneNum];
}

@end
